import { removeStora,getStora,setStora  } from '@/utils/storaOper'
import { getUserInfo } from '@/api/userApi'
import { Toast,Dialog } from 'vant'
import { doLog,doReg, } from '@/api/userApi.js'
import { Message } from 'element-ui'
export default {
  state:{
    token:null,
    info:{}
  },
  getters:{
    token:state=>state.token,
    info:state=>state.info,
    uType:state=>state.info.type,
    uid:state=>state.info.u_id,
    avator:state=>state.info.ava,
  },
  mutations:{
    SET_TOKEN:(state,token) => { state.token = token },
    REMOVE_TOKEN:(state) => { state.token = null },
    SET_INFO:(state,info) => { state.info = info },
    CHANGE_INFO:(state,data) => {
      data.map(e=>{
        const { key,value } = e
        state.info[key] = value
      })
    }
  },
  actions:{
    logOut:({commit}) => {
      commit('REMOVE_TOKEN')
      commit('SET_INFO',{})
      removeStora('SRC-TOKEN')
    },
    getInfo:async ({commit,state},token) =>{
      if(!token){
        token = state.token
        if(!token) token = getStora('SRC-TOKEN').token
      }
      if(token){
        const request = await getUserInfo({token}).catch(e=>false)
        if(!request){
          Message.warning("用户登录状态过期")
          return false
        }
        
        const { info } = request
        console.log({info})
        commit('SET_INFO',info)
        commit('SET_TOKEN',token)
        return true
      }
    }
  }
}